import { defineStore } from 'pinia'
import { getApplicationList, getApplicationById, createApplication, updateApplication, deleteApplication, submitApplication, approveApplication, rejectApplication, cancelApplication, getApplicationTasks } from '@/api/application'
import { message } from '@/utils/message'
import { APPLICATION_STATUS } from '@/utils/constants'

// 定义测试申请状态管理
const useApplicationStore = defineStore('application', {
  state: () => ({
    // 申请单列表
    applicationList: [],
    // 申请单详情
    applicationDetail: null,
    // 申请单关联的测试任务
    applicationTasks: [],
    // 分页信息
    pagination: {
      current: 1,
      size: 10,
      total: 0
    },
    // 加载状态
    loading: false,
    // 搜索条件
    searchParams: {}
  }),

  getters: {
    // 获取申请单列表
    getApplicationList: (state) => state.applicationList,
    // 获取申请单详情
    getApplicationDetail: (state) => state.applicationDetail,
    // 获取申请单关联的测试任务
    getApplicationTasks: (state) => state.applicationTasks,
    // 获取分页信息
    getPagination: (state) => state.pagination,
    // 获取加载状态
    getLoading: (state) => state.loading,
    // 获取搜索条件
    getSearchParams: (state) => state.searchParams,
    // 获取申请单状态文本
    getApplicationStatusText: (state) => (status) => {
      const statusMap = {
        [APPLICATION_STATUS.PENDING]: '待审核',
        [APPLICATION_STATUS.APPROVED]: '已批准',
        [APPLICATION_STATUS.REJECTED]: '已拒绝',
        [APPLICATION_STATUS.CANCELLED]: '已取消'
      }
      return statusMap[status] || '未知状态'
    }
  },

  actions: {
    // 获取申请单列表
    async getApplicationListAction(params = {}) {
      try {
        this.loading = true
        this.searchParams = params
        
        const response = await getApplicationList({
          page: params.page || this.pagination.current,
          pageSize: params.pageSize || this.pagination.size,
          applicationNo: params.applicationNo,
          productId: params.productId,
          status: params.status,
          createTimeStart: params.createTimeStart,
          createTimeEnd: params.createTimeEnd,
          createdBy: params.createdBy
        })
        
        this.applicationList = response.data.records || []
        this.pagination = {
          current: response.data.current || 1,
          size: response.data.size || 10,
          total: response.data.total || 0
        }
        
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '获取申请单列表失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 获取申请单详情
    async getApplicationDetailAction(applicationId) {
      try {
        this.loading = true
        const response = await getApplicationById(applicationId)
        this.applicationDetail = response.data
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '获取申请单详情失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 创建申请单
    async createApplicationAction(applicationData) {
      try {
        this.loading = true
        const response = await createApplication(applicationData)
        message.success('创建申请单成功')
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '创建申请单失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 更新申请单
    async updateApplicationAction(applicationId, applicationData) {
      try {
        this.loading = true
        const response = await updateApplication(applicationId, applicationData)
        message.success('更新申请单成功')
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '更新申请单失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 删除申请单
    async deleteApplicationAction(applicationId) {
      try {
        this.loading = true
        const response = await deleteApplication(applicationId)
        message.success('删除申请单成功')
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '删除申请单失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 提交申请单
    async submitApplicationAction(applicationId) {
      try {
        this.loading = true
        const response = await submitApplication(applicationId)
        message.success('提交申请单成功')
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '提交申请单失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 审批申请单
    async approveApplicationAction(applicationId, approvalData) {
      try {
        this.loading = true
        const response = await approveApplication(applicationId, approvalData)
        message.success('审批申请单成功')
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '审批申请单失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 拒绝申请单
    async rejectApplicationAction(applicationId, rejectionData) {
      try {
        this.loading = true
        const response = await rejectApplication(applicationId, rejectionData)
        message.success('拒绝申请单成功')
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '拒绝申请单失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 取消申请单
    async cancelApplicationAction(applicationId, cancelData) {
      try {
        this.loading = true
        const response = await cancelApplication(applicationId, cancelData)
        message.success('取消申请单成功')
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '取消申请单失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 获取申请单关联的测试任务
    async getApplicationTasksAction(applicationId, params = {}) {
      try {
        this.loading = true
        const response = await getApplicationTasks(applicationId, params)
        this.applicationTasks = response.data || []
        return Promise.resolve(response)
      } catch (error) {
        message.error(error.message || '获取测试任务列表失败')
        return Promise.reject(error)
      } finally {
        this.loading = false
      }
    },

    // 重置申请单详情
    resetApplicationDetail() {
      this.applicationDetail = null
    },

    // 重置申请单关联的测试任务
    resetApplicationTasks() {
      this.applicationTasks = []
    },

    // 重置分页信息
    resetPagination() {
      this.pagination = {
        current: 1,
        size: 10,
        total: 0
      }
    }
  }
})

export default useApplicationStore